import 'package:flutter/material.dart';

import 'package:hktk_app/pages/course/child/catalog_page.dart';
import 'package:hktk_app/pages/course/child/course_handouts_page.dart';
import 'package:hktk_app/pages/course/child/note_course_page.dart';
import 'package:hktk_app/pages/tab/study.dart';
import 'package:hktk_app/util/font_util.dart';
import 'package:hktk_app/util/view_util.dart';

/// 购买后的tab相关数据
final List<String> _list1 = ["目录", "讲义", "笔记"];

/// 课程详情内容显示
class CourseContentPage extends StatefulWidget {
  /// 栏目id
  final String columnId;

  /// 章节id
  final String chapterId;

  /// 章节名称
  final String chapterName;

  /// 视频id
  final String videoId;

  /// 免费标志，true：免费/购买，false：未购买
  final bool free;

  const CourseContentPage({
    super.key,
    required this.columnId,
    required this.chapterId,
    required this.chapterName,
    required this.videoId,
    required this.free,
  });

  @override
  State<CourseContentPage> createState() => _CourseContentPageState();
}

class _CourseContentPageState extends State<CourseContentPage>
    with TickerProviderStateMixin {
  /// tab 对应的数值
  late List<String> _tabList;

  /// tabBar控制器
  late TabController _tabController;

  @override
  void initState() {
    super.initState();
    debugPrint("courseContentPage 页面 -> initState 方法执行 ===> ");
    debugPrint("");
    _tabList = _list1;
    _tabController = TabController(
      length: _tabList.length,
      vsync: this,
    );
  }

  @override
  void dispose() {
    super.dispose();
    debugPrint("courseContentPage 页面 -> dispose 方法执行 ===> ");
    debugPrint("");
    _tabController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    debugPrint("courseContentPage 页面 -> build 方法执行 ===> ");
    debugPrint("");
    return Column(
      children: [
        _tabItem(),
        Expanded(
          child: TabBarView(
            controller: _tabController,
            children: _viewItems(),
          ),
        ),
      ],
    );
  }

  /// 滑动标题
  _tabItem() {
    return Container(
      height: 80,
      padding: EdgeInsets.symmetric(horizontal: 20, vertical: 13),
      alignment: Alignment.topLeft,
      child: _tabBarItem(),
    );
  }

  /// tabBar 实现
  _tabBarItem() {
    return TabBar(
      controller: _tabController,
      tabs: _tabsItem(),
      // tab是否可以在水平方向上滚动
      isScrollable: true,
      // 从左到右进行排列，设置tab左侧对齐，去除左侧偏移
      tabAlignment: TabAlignment.start,
      // 指示器自定义
      indicator: GradientUnderlineTabIndicator(
        colors: [Color(0xFF3C6CFF), Color(0xFFE5EBFF)], // 渐变色
        lineHeight: 3.0, // 下划线高度
      // borderRadius: BorderRadius.circular(22.0.w), // 圆角
      ),
      // 指示器的内边距
      // indicatorPadding: EdgeInsets.only(top: px8),
      // 分隔线高度
      dividerHeight: 0,
      // tab间距修改
      labelPadding: EdgeInsets.zero,
      // 点击监听
      // onTap: (index) {
      //   debugPrint("index ===> $index");
      // },
    );
  }

  /// tab标签具体实现
  _tabsItem() {
    List<Tab> tabs = [];
    for (int i = 0; i < _tabList.length; i++) {
      tabs.add(Tab(
        child: Padding(
          padding: EdgeInsets.only(left: 20, right: 20),
          child: Text(
            _tabList[i],
            style: FontUtil.fontBlack1(18),
          ),
        ),
      ));
    }
    return tabs;
  }

  /// 具体内容显示
  _viewItems() {
    List<Widget> list = [];
    for (int i = 0; i < _tabList.length; i++) {
      String title = _tabList[i];
      if (title == "目录") {
        list.add(CatalogPage(
          columnId: widget.columnId,
        ));
      } else if (title == "讲义") {
        list.add(CourseHandoutsPage(
          videoId: widget.videoId,
          columnId: widget.columnId,
          free: widget.free,
        ));
      } else if (title == "笔记") {
        list.add(NoteCoursePage(
          chapterId: widget.chapterId,
          chapterName: widget.chapterName,
          videoId: widget.videoId,
        ));
      }
    }
    return list;
  }
}
